package com.caixy.adminSystem.controller;

import com.caixy.adminSystem.annotation.AuthCheck;
import com.caixy.adminSystem.common.BaseResponse;
import com.caixy.adminSystem.common.ErrorCode;
import com.caixy.adminSystem.common.ResultUtils;
import com.caixy.adminSystem.constant.UserConstant;
import com.caixy.adminSystem.exception.BusinessException;
import com.caixy.adminSystem.model.dto.fileinfo.AddFileInfoRequest;
import com.caixy.adminSystem.model.dto.fileinfo.DeleteFileInfoRequest;
import com.caixy.adminSystem.model.dto.fileinfo.SelectFileInfoRequest;
import com.caixy.adminSystem.model.dto.fileinfo.UpdateFileInfoRequest;
import com.caixy.adminSystem.model.entity.FileInfo;
import com.caixy.adminSystem.model.entity.User;
import com.caixy.adminSystem.service.FileInfoService;
import com.caixy.adminSystem.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/fileInfo")
@Slf4j
public class FileInfoController {

    @Resource
    private FileInfoService fileInfoService;
    @Resource
    private UserService userService;

    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE )
    public BaseResponse<Long> addFileInfo(@RequestBody AddFileInfoRequest addfileInfo, HttpServletRequest request)
    {
       if (addfileInfo ==null){
           throw new BusinessException(ErrorCode.PARAMS_ERROR);
       }
        User loginUser = userService.getLoginUser(request);
        Long userId = loginUser.getId();
        addfileInfo.setUploaderId(userId);

        fileInfoService.SaveAddFileInfo(addfileInfo);
        Long newUserId=addfileInfo.getId();
        return ResultUtils.success(newUserId);
    }
    @PostMapping( "/detele")
    public BaseResponse<String> deleteFileInfo(@RequestBody DeleteFileInfoRequest deleteFileInfoRequest,HttpServletRequest httpServletRequest){
        if (deleteFileInfoRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(httpServletRequest);
        Long userId = loginUser.getId();
        deleteFileInfoRequest.setUploaderId(userId);
        fileInfoService.DeleteFileInfo(deleteFileInfoRequest);
        return ResultUtils.success("");
    }
    @PostMapping( "/update")
    public BaseResponse<String> updateFileInfo(@RequestBody UpdateFileInfoRequest updateFileInfoRequest,HttpServletRequest httpServletRequest){
        if(updateFileInfoRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        User loginUser = userService.getLoginUser(httpServletRequest);
        Long userId = loginUser.getId();
        updateFileInfoRequest.setUploaderId(userId);

        Long updateId = updateFileInfoRequest.getId();

        if (updateId== null)
        {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }


        fileInfoService.SaveUpdateFileInfo(updateFileInfoRequest);
        return ResultUtils.success("");
    }
    @PostMapping( "/select")

    public BaseResponse<String> selectFileInfo(@RequestBody SelectFileInfoRequest selectFileInfoRequest,HttpServletRequest httpServletRequest){
        if(selectFileInfoRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(httpServletRequest);
        Long userId = loginUser.getId();
        selectFileInfoRequest.setUploaderId(userId);

        fileInfoService.SelectFileInfo(selectFileInfoRequest);
        return ResultUtils.success("");
    }


}
